#!/bin/bash
#安装脚本
#基础环境安装
install_base(){       
    echo "关闭防火墙"
    systemctl stop firewalld && systemctl disable firewalld

    echo "关闭selinux"
    sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g"  /etc/selinux/config
    setenforce 0

    echo "卸载docker相关组件"
    sudo yum remove -y  docdocker-client \
	docker-client \
	docker-client-latest \
	docker-common \
	docker-latest \
	docker-latest-logrotate \
	docker-logrotate \
	docker-selinux \
	docker-engine-selinux \
	docker-engine

    echo "安装相关组件"
    sudo yum install -y yum-utils \
        device-mapper-persistent-data \
	lvm2


    cat << 'EOF' > /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
#baseurl=https://download.docker.com/linux/centos/7/$basearch/stable
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=0
EOF

    echo "安装docker-ce"
    yum install -y docker-ce.x86_64
    echo "配置加速"
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}
EOF
	
    sudo systemctl daemon-reload
    sudo systemctl restart docker

    #获取当前安装目录
    path_now=`pwd`
    path_mysql=$path_now"/mysql"
    path_chg=${path_mysql//"/"/"\/"}

    #修改数据库备份脚本并添加到定时任务
    sed -i s/install_path/$path_chg/g ./backupsql.sh
    chmod +x ./backupsql.sh
    cp ./backupsql.sh /root/backupsql.sh
    echo '0 4 * * * root /root/backupsql.sh' >> /etc/crontab



    echo "安装docker-compose"
    cp ./docker-compose  /usr/bin/docker-compose
    chmod +x /usr/bin/docker-compose

    echo "设置定时清理脚本"
    cp ./clearlog.sh /root/clearlog.sh
    chmod +x /root/clearlog.sh
    echo '0 3 * * * root /root/clearlog.sh' >> /etc/crontab
    #新建docker网络
    echo "新建docker网络中。。"
    docker network create wecomscrm
    docker network inspect wecomscrm
    sleep 2
}


monitor_sign=0
for var in $@
do
index=`echo $var| awk -F"=" '{print $1}'`;
[ $index == "--monitor" ]&&monitor_sign=1
done

docker -v||install_base
mkdir ./elastic
chmod -R 777  ./elastic
docker-compose -f ./docker-compose-es.yml up -d

if [[ $monitor_sign -eq 1 ]];then
    IP=`curl cip.cc|awk -F" " 'NR==1 {print $3}'`
    sed -i '/host_ip/d' .env
    echo "host_ip="$IP>>.env
    docker-compose -f ./docker-compose-monitor.yml up -d
fi








